package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.basicdata.dao.BaseAssetOperationDetailLogMapper;
import com.redoor.app.stake.basicdata.model.BaseAssetOperationDetailLog;
import com.redoor.app.stake.basicdata.model.BaseAssetOperationTrajectory;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseAssetOperationDetailLogCriteria;
import com.redoor.app.stake.basicdata.service.BaseAssetOperationDetailLogService;
import com.redoor.app.stake.basicdata.service.LogService;
import com.redoor.app.stake.common.model.CompareResultBean;
import com.redoor.app.stake.common.utils.CompareUtil;
import com.redoor.app.stake.common.utils.JsonUtils;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-03-03 19:26:43.
 */
@Service
public class BaseAssetOperationDetailLogServiceImpl extends AbstractService<BaseAssetOperationDetailLog, String> implements BaseAssetOperationDetailLogService {
    private Logger logger = LoggerFactory.getLogger(BaseAssetOperationDetailLogServiceImpl.class);

    @Autowired
    private BaseAssetOperationDetailLogMapper baseAssetOperationDetailLogMapper;
    @Autowired
    private LogService logService;

    @Override
    public int saveAll(List<BaseAssetOperationDetailLog> operationTrajectories) {
        if(operationTrajectories != null && operationTrajectories.size() > 0){
            // baseHsfOperationLogMapper.insertList() 只能用自增的方式新增，当前表结构无法使用
            baseAssetOperationDetailLogMapper.batchInsert(operationTrajectories);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public void addTraceDetailLogToRedis(Object oldObj, Object newObj, BaseAssetOperationTrajectory baseAssetOperationTrajectory) {
        BaseAssetOperationDetailLog baseAssetOperationDetailLog;
        List<BaseAssetOperationDetailLog> detailList = new ArrayList();
        try {
            List<CompareResultBean> attrList = CompareUtil.compareTwoClass(newObj,oldObj,null);

            for (CompareResultBean bean:attrList) {
                String attrName = bean.getName();
                baseAssetOperationDetailLog = new BaseAssetOperationDetailLog();
                baseAssetOperationDetailLog.setAttrname(attrName);
                baseAssetOperationDetailLog.setNewvalue(bean.getNewValue().toString());
                baseAssetOperationDetailLog.setOldvalue(bean.getOldValue().toString());
                baseAssetOperationDetailLog.setIdx(detailList.size()+"");
                baseAssetOperationDetailLog.setTrajectoryuuid(baseAssetOperationTrajectory.getUuid());
                baseAssetOperationDetailLog.setOpertordate(new Date());
                baseAssetOperationDetailLog.setUuid(UUIDTool.getUUID());

                logService.addLogToRedis(baseAssetOperationDetailLog, LogEnum.ASSET_TRACE_DETAIL_LOG);
            }

        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
    }

    @Override
    public List<BaseAssetOperationDetailLog> getTraceDetailList(String traceUuid) {
        BaseAssetOperationDetailLogCriteria param = new BaseAssetOperationDetailLogCriteria();
        param.setTrajectoryUuid(traceUuid);
        return baseAssetOperationDetailLogMapper.getTraceDetailList(param);
    }

}
